package com.wu.family.more;

import java.net.URL;
import java.util.Random;

import javax.activation.MailcapCommandMap;

import org.json.JSONObject;

import com.mobclick.android.MobclickAgent;
import com.wu.family.BaseActivity;
import com.wu.family.R;
import com.wu.family.config.UserInfo;
import com.wu.family.feed.FeedListAdapter;
import com.wu.family.guid.GuidActivity;
import com.wu.family.model.Entity;
import com.wu.family.utils.LoadImgTask;
import com.wu.family.utils.WebViewUtil;
import com.wu.family.utils.WebViewUtil.OnWebLoadLintener;
import com.wu.family.views.MyListView;

import android.R.string;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class MoreWebActivity extends BaseActivity implements OnClickListener{
	public static class WebsiteInfo extends Entity{
		private static final long serialVersionUID = -8681639275816877842L;
		private String url = "";
		private String title = "";
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
	}
	private LinearLayout llLlTitle;
	private TextView tvTvTitle;
	private WebView webView;
	private RelativeLayout rlRlBottom;
	private ImageButton ibBtnBack;
	private ImageButton ibBtnWebRefresh;
	private ImageButton ibBtnWebBack;
	private ImageButton ibBtnWebForword;
	WebsiteInfo websiteInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.onError(this);
        MobclickAgent.update(this); 
        MobclickAgent.setUpdateOnlyWifi(false);
        setContentView(R.layout.more_web);
        
        dealIntent();
        initView();
        initEvent();
	}

	private void dealIntent() {
		websiteInfo = (WebsiteInfo) getIntent().getSerializableExtra("WebsiteInfo");
		websiteInfo = websiteInfo!=null?websiteInfo:new WebsiteInfo();
	}

	private void initView() {
		llLlTitle = (LinearLayout) findViewById( R.id.llTitle );
		tvTvTitle = (TextView) findViewById( R.id.tvTitle );
		webView = (WebView) findViewById( R.id.webView );
		rlRlBottom = (RelativeLayout) findViewById( R.id.rlBottom );
		ibBtnBack = (ImageButton) findViewById( R.id.btnBack );
		ibBtnWebRefresh = (ImageButton) findViewById( R.id.btnWebRefresh );
		ibBtnWebBack = (ImageButton) findViewById( R.id.btnWebBack );
		ibBtnWebForword = (ImageButton) findViewById( R.id.btnWebForword );

		tvTvTitle.setText(websiteInfo.getTitle());
		WebViewUtil.config(context, webView, new OnWebLoadLintener() {
			@Override
			public void onReceivedTitle(String title) {}
			@Override
			public void onProgressChanged(int newProgress) {}
			@Override
			public void onPageStarted(String url) {
				showLoadingDialog();
			}
			@Override
			public void onPageFinished(String url) {
				hideLoadingDialog();
			}
			public boolean onAppForwordUrl(String url) {
				/*if (url.matches(".*"+Constant.WebAppURL.TAILY_TOPIC)) {
					IConfigInfo configInfo = new IConfigInfo();
					configInfo.setIdtype(Constant.IdType.PHOTO_ID);
					configInfo.setTopicid((String)feedContentMap.get("topicid"));
					configInfo.setJoinTopic(true);
					
					showSelPhotoDialogToPub(configInfo, null, true);
					return true;
				}*/
				return false;
			}
		});
		webView.loadUrl(websiteInfo.getUrl());
	}

	private void initEvent() {
		ibBtnBack.setOnClickListener( this );
		ibBtnWebRefresh.setOnClickListener( this );
		ibBtnWebBack.setOnClickListener( this );
		ibBtnWebForword.setOnClickListener( this );
	}
	
	@Override
	public void onClick(View v) {
		if (v == ibBtnBack) {
			finish();
		}else if (v == ibBtnWebRefresh) {
			webView.reload();
		}else if (v == ibBtnWebBack && webView.canGoBack()) {
			webView.goBack();
		}else if (v == ibBtnWebForword && webView.canGoForward()) {
			webView.goForward();
		}
	}
	
	
}
